package base.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 网络工具
 *
 * @author liujia
 */
public class InetUtils {
    private static final Logger logger = LoggerFactory.getLogger(InetUtils.class);

    private static final String UNKNOWN_IP_STR = "unknown";
    private static final String LOOPBACK_IP_STR = "127.0.0.1";

    /**
     * 获取客户端的真实IP
     */
    public static String getClientRealIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isBlank(ip) || UNKNOWN_IP_STR.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip)  || UNKNOWN_IP_STR.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip)  || UNKNOWN_IP_STR.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 多个IP按照','分割，对于通过多个代理的情况，第一个IP为客户端真实IP
        // '***.***.***.***'.length() = 15
        if (null != ip && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        return ip;
    }

    /**
     * 获取Referer
     */
    public static String getReferer(HttpServletRequest request) {
        return request.getHeader("Referer");
    }

    /**
     *  获取本机用户名
     */
    public static String getLocalHostName() {
        String hostName = "";
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            logger.error("", e);
        }
        return hostName;
    }

    /**
     *  获取本机IP
     */
    public static String getLocalIp() {
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            logger.error("", e);
        }
        return ip;
    }

    /**
     *  获取本机网卡IP地址
     */
    public static String getLocalIpByNetCard() {
        try {

            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        String s = ip.getHostAddress();
                        if (LOOPBACK_IP_STR.equals(s)) {
                            continue;
                        }
                        return ip.getHostAddress();
                    }
                }
            }
            return LOOPBACK_IP_STR;
        } catch (SocketException e) {
            return LOOPBACK_IP_STR;
        }
    }
}
